package com.hqyj.bigdata.hiveproject.util;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class HiveUtil {

    //把本地数据文件导入到指定的表中。
    public static void importData(String path, String tableName) throws SQLException {
        String hql = "load data local inpath '"+path+"' into table "+tableName;
        PreparedStatement pstm = HiveConnection.getConnection().prepareStatement(hql);
        boolean flag = pstm.execute(hql);

        pstm.close();
    }

    //执行DML操作
    public static void execute(String hql) throws SQLException {
        PreparedStatement pstm = HiveConnection.getConnection().prepareStatement(hql);
        boolean flag = pstm.execute(hql);

        pstm.close();

    }

    //执行DQL操作
    public static void query(String hql) throws SQLException {
        //String sql = "select name,addr.province,addr.city,addr.street from t_person";
        PreparedStatement pstm = HiveConnection.getConnection().prepareStatement(hql);
        ResultSet rs = pstm.executeQuery(hql);

        ResultSetMetaData rsmd = rs.getMetaData();
        //获取字段的个数
        int filedCount = rsmd.getColumnCount();
        StringBuilder sb = new StringBuilder();
        while (rs.next()) {
            for(int i=1;i<=filedCount;i++){
                sb.append(rs.getString(i)+" ");
            }
            sb.append("\r\n");
        }
        System.out.println(sb);
        pstm.close();
        rs.close();
    }
}
